/**
 * Copyright 2018 lenos
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.istartfinancial.listener;


import cn.istartfinancial.constant.FlowableConstant;
import cn.istartfinancial.entity.flowable.ActAssignee;
import cn.istartfinancial.service.flowable.ActAssigneeService;
import cn.istartfinancial.service.flowable.impl.ActAssigneeServiceImpl;
import cn.istartfinancial.util.SpringUtil;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import java.util.List;

/**
 * @author: Nzp
 * @date: 2018/8/29 17:25
 * @description: 流程监听器 动态注入节点办理人
 */
public class ActNodeListener implements TaskListener
{

    @Override
    public void notify(DelegateTask delegateTask)
    {
        //KEY
        String nodeId = delegateTask.getTaskDefinitionKey();
        ActAssigneeService actAssigneeService = SpringUtil.getBean(ActAssigneeServiceImpl.class);
        List<ActAssignee> assigneeList = actAssigneeService.select(new ActAssignee(nodeId));
        for (ActAssignee assignee : assigneeList)
        {
            switch (assignee.getAssigneeType())
            {
                case FlowableConstant.GROUP_TYPE:
                    delegateTask.addCandidateGroup(assignee.getRoleId());
                    break;
                case FlowableConstant.USER_TYPE:
                    delegateTask.addCandidateUser(assignee.getAssignee());
                    break;
            }
        }
    }
}
